//package com.wing.db.interceptor;//package com.wing.interceptor;
//
//
//import com.wing.web.db.config.util.DynamicDataSource;
//import com.wing.web.utils.JwtUtils;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang.StringUtils;
//import org.springframework.stereotype.Component;
//import org.springframework.web.servlet.AsyncHandlerInterceptor;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.sql.*;
//import java.util.ArrayList;
//import java.util.List;
//
///**
// * 接口拦截器
// *
// * @author
// */
//@Component
//@Slf4j
//public class InterfaceInterceptor implements AsyncHandlerInterceptor {
//
//    private static final String address = "124.232.154.35";
//    private static final String port = "3306";
//
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
////		log.info("拦截器....{}", request.getServletPath());
////
////        StringBuffer url = request.getRequestURL();
////        String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(request.getSession().getServletContext().getContextPath()).toString();
////        String domain = tempContextUrl.split("://")[1];
////        if(domain.indexOf(":") != -1){
////            domain = domain.split(":")[0];
////        }
////        log.info("拦截器域名....{}", domain);
////
////        // 微信小程序域名
////        String wxDomain = "xcx.madeinchina.ltd";
////        List<String> urlList = new ArrayList<>(); // 接口过滤集合
////        urlList.add("/user/app/user/loginByPhone");
////        urlList.add("/user/app/user/weChatLogin");
//////        urlList.add("/system/admin/systemOfficialInfo/getInfo");
//////        urlList.add("/user/admin/client/getByClientId");
//////        urlList.add("/user/admin/user/getByUsername");
//////        urlList.add("/user/admin/authority/authorityRoleList");
////        log.info("拦截手机号....{}", request.getParameter("phone"));
////
////        if(domain.indexOf("madeinchina.ltd") != -1){ // 请求接口存在域名
////            if(domain.equals(wxDomain)){
////                // 截取不需要登录的接口
////                if(urlList.contains(request.getServletPath())){
////                    // 获取传过来的手机号参数
////                    String phone = request.getParameter("phone");
////                    // 查询手机号对应的数据源
////                    String database = queryDataSourceConfig(null, phone);
////                    if(StringUtils.isNotBlank(database)){
////                        if(request.getServletPath().indexOf("/user") == 0){
////                            DynamicDataSource.name.set("user-"+database);
////                        }else{
////                            DynamicDataSource.name.set(database);
////                        }
////                    }
////                }else{
////                    try {
////                        DynamicDataSource.name.set(JwtUtils.getEnterpriseCode());
////                    } catch (Exception e) {
////                        log.info("1.该接口未传token; 2.如果该接口不需要token，请将接口加入到拦截器过滤集合中，接口拦截器(InterfaceInterceptor)，例子:(urlList.add(\"/user/admin/user/getByUsername\");)");
////                        return false;
////                    }
////                }
////            }else{
////                // 通过域名查询关联的数据库
////                String database = queryDataSourceConfig(domain, null);
////
////                if(StringUtils.isNotBlank(database)){
////                    if(request.getServletPath().indexOf("/user") == 0){
////                        DynamicDataSource.name.set("user-"+database);
////                    }else{
////                        DynamicDataSource.name.set(database);
////                    }
////                }
////            }
////        }else{
////
//////            DynamicDataSource.name.set(JwtUtils.getEnterpriseCode());
////        }
//        return true;
//    }
//
//    private String queryDataSourceConfig(String domain, String phone) {
//        String url = "jdbc:mysql://"+address+":"+port+"/szgc-enterprise-user?characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC&serverTimezone=Asia/Shanghai";
//        String username = "root";
//        String password = "Root@7Ud*&d#78Rc23Dfd^";
//
//        Connection newConn = null; // 连接刚刚创建的数据库
//        String name = "";
//        try {
//            newConn = DriverManager.getConnection(url, username, password);
//            Statement newSmt = newConn.createStatement();
//            String sql = "";
//            if(StringUtils.isNotBlank(domain)){
//                sql = "select * from sys_data_source where domain = " + '"' + domain + '"';
//                ResultSet resultSet = newSmt.executeQuery(sql);
//                while (resultSet.next()){
//                    name = resultSet.getString("name");
//                }
//            }else if(StringUtils.isNotBlank(phone)){
//                sql = "select * from sys_phone_code where phone = " + '"' + phone + '"';
//                ResultSet resultSet = newSmt.executeQuery(sql);
//                while (resultSet.next()){
//                    name = resultSet.getString("code");
//                }
//            }
//            newSmt.close();
//            newConn.close();
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return name;
//    }
//
//}
